package com.xxd.dfs.namenode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author: XiaoDong.Xie
 * @create: 2020-08-14 09:12
 * @description: 代表一个NameNode进程，是整个分布式文件系统的master角色，项目架构master-slaves
 * 1、负责调度dfs-datanode的任务
 * 2、负责整个文件目录的维护(namespace)
 */
public class NameNode {

    private FSNameSystem fsNameSystem;

    private NameNodeRpcProto rpcServer;

    private FSImageReceiveThread fsImageReceiveThread;
    private final static Logger logger = LoggerFactory.getLogger(NameNode.class);

    public NameNode() {
        Configuration conf = Configuration.getInstance();
        this.fsNameSystem = new FSNameSystem();
        String fsimageFileName = "";
        try {
            fsimageFileName = fsNameSystem.loadFromDisk(conf);
        } catch (IOException e) {
            logger.error("从磁盘恢复数据失败", e);
            System.exit(1);
        }

        this.rpcServer = new NameNodeRpcProto(fsNameSystem);
        this.fsImageReceiveThread = new FSImageReceiveThread(fsNameSystem, fsimageFileName);
        fsNameSystem.setProto(rpcServer);
    }

    /**
     * 让NameNode运行起来
     */
    private void start() throws Exception {
        this.rpcServer.start();
        this.fsImageReceiveThread.start();

        this.rpcServer.blockUntilShutdown();
    }


    public static void main(String[] args) throws Exception {
        NameNode nameNode = new NameNode();
        nameNode.start();
    }
}
